using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace Dare.Utilities
{
    public class EnumDisplayNameAttribute : Attribute
    {
        private static Dictionary<Enum, string> dictCacheNames;
        static EnumDisplayNameAttribute()
        {
            dictCacheNames = new Dictionary<Enum, string>(200);
        }

        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public EnumDisplayNameAttribute(string name)
            : base()
        {
            this.name = name;
        }

        public override string ToString()
        {
            return name;
        }

        
        public static string GetDisplayName(Enum enumSubitem)
        {
            if (dictCacheNames.ContainsKey(enumSubitem))
            {
                return dictCacheNames[enumSubitem];
            }
            else
            {
                string name = enumSubitem.ToString();
                FieldInfo fieldinfo = enumSubitem.GetType().GetField(name);
                object[] objs = fieldinfo.GetCustomAttributes(typeof(EnumDisplayNameAttribute), false);
                if (objs.Length > 0)
                {
                    name = ((EnumDisplayNameAttribute)objs[0]).Name;
                }
                
                dictCacheNames[enumSubitem] = name;
                return name;
            }
        }
    }
}
